/*
 * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
 *
 * This file is part of FileExplorer.
 *
 * FileExplorer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * FileExplorer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with SwiFTP.  If not, see <http://www.gnu.org/licenses/>.
 */
package cn.cheln.support.images;

import android.content.Context;
import android.widget.ImageView;

import java.io.File;

import cn.cheln.explorer.beans.FileInfo;
import cn.cheln.support.images.FileCategoryHelper.FileCategory;

public class FileIconHelper {

	private FileIconLoader mIconLoader;

	public FileIconHelper(Context context) {
		mIconLoader = new FileIconLoader(context);
	}

	public void setIcon(FileInfo thumbnailBase, ImageView fileImage) {
		String filePath = thumbnailBase.path;
		long fileId = thumbnailBase.dbId;
		FileTypeInfo type = null;
		if (thumbnailBase.isForceDir()) {
			type = new FileTypeInfo(FileCategory.Folder, FileInfo.iconDir, null);
		} else {
			type = FileCategoryHelper.getCategoryFromPath(filePath);
		}
		FileCategory fc = type.fc;
		boolean set = false;
		mIconLoader.cancelRequest(fileImage);
		switch (fc) {
		case Music:
			if (thumbnailBase.thumbnailPath != null) {
//				if (new File(thumbnailBase.thumbnailPath).exists()) {
					filePath = thumbnailBase.thumbnailPath;
//				}
			}
		case Video:
		case Apk:
		case Picture:
			set = mIconLoader.loadIcon(fileImage, thumbnailBase.path, fileId, type);
			break;
		}
		if (!set) {
			fileImage.setImageBitmap(type.defaultBg);
			set = true;
		}
	}
}
